package com.xckj.utils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import com.adrninistrator.jacg.common.enums.ConfigDbKeyEnum;
import com.adrninistrator.jacg.common.enums.OtherConfigFileUseSetEnum;
import com.adrninistrator.jacg.conf.ConfigureWrapper;
import com.adrninistrator.jacg.util.JACGFileUtil;
import com.adrninistrator.jacg.util.JACGUtil;
/**
 * 配置文件的工具类
 */
public class WriteConfigFile {
    private static final Logger log = LoggerFactory.getLogger(WriteConfigFile.class);
    
    /**
     * 模拟向 properties 文件写入数据
     */
    public static boolean setParam(String confFile, String key, String value){
    	boolean isSucc = true;
        try {
        	String rootPath = new ClassPathResource(confFile).getURL().getPath();
        	rootPath = URLDecoder.decode(rootPath ,"utf-8");
            PropertiesConfiguration propsConfig =  new PropertiesConfiguration(rootPath);
            propsConfig.setEncoding("utf-8");
            // 修改属性之后自动保存，省去了propsConfig.save()过程
            propsConfig.setAutoSave(true);
            // setProperty：遇到同名key会替换value，没有则新增
            propsConfig.setProperty(key, value);
        }catch (Exception e) {
        	log.error(e.getMessage(), e);
        	isSucc = false;
        }
        return isSucc;
    }
    
    /**
     * 模拟向 properties 文件写入数据
     */
    public static void addParam(String confFile, String key, String value){
        try {
        	String rootPath = new ClassPathResource(confFile).getURL().getPath();
        	rootPath = URLDecoder.decode(rootPath ,"utf-8");
            PropertiesConfiguration propsConfig =  new PropertiesConfiguration(rootPath);
            propsConfig.setEncoding("utf-8");
            // 修改属性之后自动保存，省去了propsConfig.save()过程
            propsConfig.setAutoSave(true);
            // addProperty：只会新增，即使遇到遇到同名key也不会替换
            propsConfig.addProperty(key, value);
        }catch (Exception e) {
        	log.error(e.getMessage(), e);
        }
    }
 
    /**
     * 模拟向 properties 文件读取数据
     */
    public static String getParam(String confFile, String key){
    	String value = "";
        try {
        	String rootPath = new ClassPathResource(confFile).getURL().getPath();
        	rootPath = URLDecoder.decode(rootPath ,"utf-8");
            PropertiesConfiguration propsConfig =  new PropertiesConfiguration(rootPath);
            propsConfig.setEncoding("utf-8");
            value = propsConfig.getString(key);
        }catch (Exception e) {
        	log.error(e.getMessage(), e);
        }
        return value;
    }
    
    /**
     * 写入配置文件
     * @param key
     * @param value
     * @param comments
     * @return
     */
	public static boolean writeToConf(String confFile, String key, String value, String comments) {
		boolean isSucc = true;
        Properties properties = new Properties();
        // 设置配置参数
        properties.setProperty(key, value);
        try {
        	String rootPath = new ClassPathResource(confFile).getURL().getPath();
			rootPath = URLDecoder.decode(rootPath ,"utf-8");

        	// 写入配置文件
        	properties.store(new FileOutputStream(rootPath), "My Configuration");
        	log.error("配置文件写入成功");
        } catch (Exception e) {
        	log.error(e.getMessage(), e);
        	isSucc = false;
        }
        return isSucc;
	}
	
    public static void main(String[] args) {
    	ConfigureWrapper configureWrapper = new ConfigureWrapper(false);
    	String username = configureWrapper.getMainConfig(ConfigDbKeyEnum.CDKE_DB_USERNAME);
    	System.out.println("user:" + username);
    	String confFile = OtherConfigFileUseSetEnum.OCFUSE_ALLOWED_CLASS_PREFIX.getKey();
//		writeToConf(confFile, "", "com.stock.qfii", "testcomments");	
    	setParam(confFile, "", "com.stock.qfii3");
    	String value = getParam(confFile, "");
    	System.out.println("value:" + value);
    	
    	Set<String> configSet = JACGFileUtil.readFile2Set(JACGUtil.getInputRootPath() + confFile);
    	System.out.println("set:" + configSet);

    }
}